package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.tradewaractivity.TradeWarActivityProto;
import com.yanqu.road.server.logic.activity.tradewar.TradeWarActivityData;

import java.util.List;
import java.util.Map;

public class TradeWarActivityPb {

    public static TradeWarActivityProto.TradeWarActivityMsg.Builder parseTradeWarActivityMsg(ActivityInfo activityInfo,
            Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap, Map<Integer, MallInfo> activityMallInfoMap, String language){
        if(null == activityInfo) {
            return null;
        }
        TradeWarActivityProto.TradeWarActivityMsg.Builder resultMsg = TradeWarActivityProto.TradeWarActivityMsg.newBuilder();
        //活动
        ActivityProto.ActivityTempMsg.Builder activityMsg = ActivityPb.parseActivityTempMsg(activityInfo, language);
        if (null != activityMsg) {
            resultMsg.setActivityInfo(activityMsg);
        }

        //活动条件
        if(null != activityConditionInfoMap) {
            for (List<ActivityConditionInfo> conditionInfoList : activityConditionInfoMap.values()) {
                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    CommonActivityProto.CommonActivityConditionTempMsg.Builder conditionMsg = CommonActivityPb.parseCommonActivityConditionTempMsg(conditionInfo, language);
                    if (null != conditionMsg) {
                        resultMsg.addActivityConditionInfoList(conditionMsg);
                    }
                }
            }
        }

        //活动商品
        if(null != activityMallInfoMap) {
            for (MallInfo mallInfo : activityMallInfoMap.values()) {
                CommonProto.MallTempMsg.Builder mallMsg = CommonPb.parseMallTempMsg(mallInfo, language);
                if (null != mallMsg) {
                    resultMsg.addActivityMallInfoList(mallMsg);
                }
            }
        }

        return resultMsg;
    }

    public static TradeWarActivityProto.TradeWarActivityDetailMsg.Builder parseTradeWarActivityDetailMsg(Map<Integer, TradeWarActivityData> activityDataMap,
             ActivityInfo activityInfo, Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap, Map<Integer, MallInfo> activityMallInfoMap, String language){
        TradeWarActivityProto.TradeWarActivityDetailMsg.Builder resultMsg = TradeWarActivityProto.TradeWarActivityDetailMsg.newBuilder();
        if(null == activityInfo){
            return null;
        }
        TradeWarActivityProto.TradeWarActivityMsg.Builder activityMsg = parseTradeWarActivityMsg(activityInfo, activityConditionInfoMap, activityMallInfoMap, language);
        if (null != activityMsg) {
            resultMsg.setActivityInfo(activityMsg);
        }

        //活动数据
        CommonActivityProto.CommonActivityDataMsg.Builder activityDataMsg = parseCommonActivityDataMsg(activityDataMap);
        if(null != activityDataMsg){
            resultMsg.setActivityData(activityDataMsg);
        }

        return resultMsg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(Map<Integer, TradeWarActivityData> activityDataMap){
        if(null == activityDataMap){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (TradeWarActivityData activityData : activityDataMap.values()) {
            for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
                CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
                if (null != conditionDataMsg) {
                    msg.addConditionDataList(conditionDataMsg);
                }
            }
        }
        return msg;
    }

    public static CommonActivityProto.CommonActivityDataMsg.Builder parseCommonActivityDataMsg(TradeWarActivityData activityData){
        if(null == activityData){
            return null;
        }
        CommonActivityProto.CommonActivityDataMsg.Builder msg = CommonActivityProto.CommonActivityDataMsg.newBuilder();
        for (UserActivityConditionData conditionData : activityData.getUserActivityConditionDataList()) {
            CommonActivityProto.CommonActivityConditionDataMsg.Builder conditionDataMsg = CommonActivityPb.parseCommonActivityConditionDataMsg(conditionData);
            if (null != conditionDataMsg) {
                msg.addConditionDataList(conditionDataMsg);
            }
        }
        return msg;
    }

}
